

from .base_handler import BaseRequestHandler
from service.utility.encryption import passwd_enc
from service.utility.data_tool import clean_dict_none


class TDPUserHandler(BaseRequestHandler):
    async def get_add(self, *args, **kwargs):
        username = self.get_argument('username')
        real_name = self.get_argument('realName')
        password = self.get_argument('password')
        email = self.get_argument('email', '')
        mobile = self.get_argument('mobile', '')
        mark = self.get_argument('mark', '')
        await self.mdb.insert_by_dict(
            table='tdp_user',
            data={
                'user_name': username,
                'real_name': real_name,
                'passwd': passwd_enc(password),
                'email': email,
                'mobile': mobile,
                'mark': mark
            }
        )

    async def get_update(self, *args, **kwargs):
        user_id = self.get_argument('userID')
        user_info = await self.mdb.query_one(
            'select id, user_name, real_name, passwd, email, mobile, mark from tdp_user where id=%s', user_id)
        username = self.get_argument('username', None)
        real_name = self.get_argument('realName', None)
        password = self.get_argument('password', None)
        email = self.get_argument('email', None)
        mobile = self.get_argument('mobile', None)
        mark = self.get_argument('mark', None)
        d = clean_dict_none({
            'user_name': username,
            'real_name': real_name,
            'email': email,
            'mobile': mobile,
            'mark': mark
        })
        if password:
            d['passwd'] = passwd_enc(password)
        user_info.update(d)
        await self.mdb.update_by_dict(
            table='tdp_user',
            data=user_info,
            where={'id': user_id}
        )

    async def get_del(self, *args, **kwargs):
        user_id = self.get_argument('userID')
        await self.mdb.delete_by_dict(
            table='tdp_user',
            where={'id': user_id}
        )




